package demo4_内部类;
class OuterClass{
    private int data1 = 1;
    private int data2 = 2;
    //实例内部类
    class InnerClass{
        private int data1 = 11; //1若同名，则优先访问内部类自己的
        private int data3 = 3;
        private int data4 = 4;
        //public static int data5;  //2不可定义static修饰的类型
        public static final int data6 = 6;//可定义常量
        public void test(){
            System.out.println(data3);
            System.out.println(data4);
            System.out.println("这是内部类的test方法");
        }
    }
    public void test(){
        System.out.println(data1);
        System.out.println(data2);
        System.out.println("这是外部类的test方法");
    }

    //静态内部类，不依赖于外部类的实现
    static class InnerClassA{
        private int data7 = 7;
        private int data8 = 8;
        //此处就可定义静态变量
        public static int data9 = 9;
        public void test(){
            System.out.println(data7);
            System.out.println(data8);
            System.out.println(data9);
            System.out.println("这是静态内部类的test方法");
        }
    }
}

interface A{
    void testA();
}
public class Test {
    public static void main(String[] args) {
        int val = 10;
        val = 100;
        //匿名内部类
        A a = new A() {
            @Override
            public void testA() {
                //默认在这里能访问的是被final修饰的不能被修改的变量
                //System.out.println(val);    //在匿名内部类中只能访问没有被修改过的值-->变量的补货
                System.out.println("haha");
            }
        };
        a.testA();

        //3 实例内部类对象实例化
        OuterClass outerClass = new OuterClass();
        OuterClass.InnerClass innerClass = outerClass.new InnerClass();
        outerClass.test();
        innerClass.test();

        //4 静态内部类对象实例化
        OuterClass.InnerClassA innerClass1 = new OuterClass.InnerClassA();
        innerClass1.test();
    }

}
